<template>
  <div class="container">
    <!-- Loading 组件：异步请求处理中显示 -->
    <Loading v-if="isLoading" />

    <!-- Modal 组件：用于显示成功或者错误提示 -->
    <Modal :visible="modalVisible" :title="modalTitle" @confirm="closeModal" @cancel="closeModal">
      <div>{{ modalMessage }}</div>
    </Modal>

    <!-- Snackbar 组件：用于显示快速提示 -->
    <Snackbar 
      :visible="snackbarVisible" 
      :message="snackbarMessage" 
      :type="snackbarType"
      @hide="hideSnackbar"
    />

    <!-- 账单类型标签 -->
    <div class="operator-toggle card">
      <button
          class="operator-btn"
          :class="{ active: currentOperator === '-' }"
          @click="setOperator('-')"
          data-type="expense"
      >
        支出
      </button>
      <button
          class="operator-btn"
          :class="{ active: currentOperator === '+' }"
          @click="setOperator('+')"
          data-type="income"
      >
        收入
      </button>
    </div>

    <!-- 分类选择 -->
    <div class="category-container card">
      <button
          v-for="category in categories"
          :key="category.name"
          @click="selectedCategory = category.name"
          class="category-btn"
          :class="{ active: selectedCategory === category.name }"
      >
        <div class="category-icon">
          {{ category.icon }}
        </div>
        <span class="category-text">{{ category.name }}</span>
      </button>
    </div>

    <!-- 日期选择 -->
    <div class="date-container card">
      <label for="bill-date" class="input-label">请选择日期:</label>
      <input type="date" id="bill-date" v-model="selectedDate" class="input-box-date" />
    </div>

    <!-- 备注输入 -->
    <div class="remark-container card">
      <label for="remark" class="input-label">备注:</label>
      <input
          type="text"
          id="remark"
          v-model="remark"
          class="input-box"
          placeholder="请输入备注"
      />
    </div>

    <!-- 金额输入区域及数字键盘 -->
    <div class="keyboard-container card">
      <!-- 金额输入框，带默认操作符显示 -->
      <div class="input-container" :class="{'expense': currentOperator === '-', 'income': currentOperator === '+'}">
        <span class="default-operator">{{ currentOperator }}</span>
        <input
            v-model="inputValue"
            type="text"
            class="input-box"
            placeholder="请输入金额表达式"
            :readonly="isMobile"
            @keyup.enter="onComplete"
        />
      </div>
      <!-- 数字键盘网格 -->
      <div class="keyboard-grid">
        <div
            class="key-row"
            v-for="(row, rowIndex) in keypadRows"
            :key="rowIndex"
            :style="{ gridTemplateColumns: 'repeat(' + row.length + ', 1fr)' }"
        >
          <button
              v-for="key in row"
              :key="key"
              class="key-btn"
              @click="appendKey(key)"
          >
            {{ key }}
          </button>
        </div>
      </div>

      <!-- 操作按钮 -->
      <div class="action-buttons">
        <button class="calc-btn button" @click="onComplete">完成</button>
        <button class="btn-delete" @click="deleteLast">删除</button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useAddPage } from "./AddPage";
import Loading from "../../components/Loading/Loading.vue";
import Modal from "../../components/Modal/Modal.vue";
import Snackbar from "../../components/Snackbar/Snackbar.vue";

const {
  categories,
  selectedCategory,
  inputValue,
  keypadRows,
  appendKey,
  deleteLast,
  onComplete,
  isMobile,
  selectedDate,
  remark,
  isLoading,
  modalVisible,
  modalTitle,
  modalMessage,
  closeModal,
  currentOperator,
  setOperator,
  snackbarVisible,
  snackbarMessage,
  snackbarType,
  hideSnackbar,
} = useAddPage();
</script>

<style scoped>
/* 整体布局 */
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px;
  min-height: 100vh;
  box-sizing: border-box;
}

/* 卡片样式 */
.card {
  background: #fff;
  border-radius: 8px;
  padding: 12px;
  margin: 10px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 600px;
}

/* 账单类型标签 */
.operator-toggle {
  display: flex;
  gap: 10px;
  justify-content: center;
}
.operator-btn {
  padding: 8px 12px;
  font-size: 16px;
  border: 1px solid #ddd;
  border-radius: 5px;
  background: #fff;
  cursor: pointer;
  transition: background 0.3s, border-color 0.3s;
}
.operator-btn.active {
  color: #fff;
  border-color: transparent;
}
.operator-btn[data-type="expense"].active {
  background: var(--primary-color-first);
}
.operator-btn[data-type="income"].active {
  background: var(--primary-color-second);
}

/* 分类选择 */
.category-container {
  display: flex;
  gap: 15px;
  flex-wrap: wrap;
  justify-content: center;
}
.category-btn {
  background: none;
  border: none;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.category-btn.active .category-icon {
  border: 2px solid;
  border-image: var(--primary-gradient) 1;
}
.category-icon {
  width: 50px;
  height: 50px;
  border: 2px solid #ddd;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
}
.category-text {
  margin-top: 5px;
  font-size: 14px;
}

/* 日期和备注输入 */
.date-container,
.remark-container {
  width: 100%;
  max-width: 600px;
  display: flex;
  flex-direction: column;
  margin-bottom: 10px;
}
.input-label {
  margin-bottom: 5px;
  font-size: 14px;
  color: #333;
}
.input-box-date,
.input-box {
  width: 100%;
  padding: 12px 16px;
  font-size: 16px;
  border: 1px solid #ddd;
  border-radius: 5px;
  box-sizing: border-box;
}

/* 金额输入区域 */
.input-container {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
.default-operator {
  font-size: 24px;
  margin-right: 8px;
  width: 30px;
  text-align: center;
}
.input-container.expense .default-operator {
  color: var(--primary-color-first);
}
.input-container.income .default-operator {
  color: var(--primary-color-second);
}
.input-container.expense .input-box {
  border-color: var(--primary-color-first);
}
.input-container.income .input-box {
  border-color: var(--primary-color-second);
}

/* 数字键盘 */
.keyboard-grid {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.key-row {
  display: grid;
  gap: 6px;
}
.key-btn {
  padding: 14px;
  font-size: 18px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 5px;
  cursor: pointer;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}
.calc-btn {
  flex: 1;
}
.button {
  background: var(--primary-gradient);
  border: none;
  color: white;
  padding: 12px 0;
  border-radius: 8px;
  font-weight: bold;
  transition: background 0.3s;
  width: 100%;
  cursor: pointer;
}
.button:hover {
  background: var(--primary-gradient-hover);
}
.btn-delete {
  background: #f56c6c;
  color: #fff;
  border-radius: 8px;
  padding: 12px 10px;
  border: none;
  cursor: pointer;
}

/* 响应式适配 */
@media (max-width: 768px) {
  .container {
    padding: 0 10px;
  }
}
</style>